<script lang="ts">
import { defineComponent, onMounted, reactive, ref } from "vue";
import { Storage } from "@/utils/database";
import { Router } from "@/utils/router";

const storeKey = "guider-store-last-store";

export default defineComponent({
  setup() {
    const tempData = { id: "" };

    const visible = ref(false);

    const storeData = reactive({ img: "", name: "" });

    onMounted(() => {
      const _storeData = Storage.getSync(storeKey);

      if (_storeData) {
        storeData.img = _storeData.img;
        storeData.name = _storeData.name;
        visible.value = true;
        tempData.id = _storeData.id;
      }
    });

    const storeClick = (index: number) => {
      if (index === -1) {
        visible.value = false;
        Storage.deleteSync(storeKey);
      } else {
        Router.navigateTo(
          `/package/guider/pages/store/index?id=${tempData.id}`
        );
      }
    };

    return { visible, storeData, storeClick };
  },
});
</script>

<template>
  <view class="last-store" v-if="visible" @click="storeClick">
    <image class="last-store-img" :src="storeData.img" mode="aspectFill" />
    <view class="last-store-content">
      <view class="last-store-name">{{ storeData.name }}</view>
      <view class="last-store-desc">是否返回上次浏览的店铺？</view>
    </view>
    <view class="last-store-close" @click.stop="storeClick(-1)">
      <icon type="clear" size="38rpx" color="#999999" />
    </view>
  </view>
</template>

<style lang="scss">
.last-store {
  position: fixed;
  left: 30rpx;
  right: 30rpx;
  bottom: 44rpx;
  display: flex;
  align-items: center;
  padding: 24rpx 40rpx;
  border-radius: 20rpx;
  background: linear-gradient(179deg, #ffdda0 0%, #ffffff 89%);

  .last-store-img {
    flex-shrink: 0;
    width: 98rpx;
    height: 98rpx;
    border-radius: 50%;
    border: 2rpx solid #ffffff;
  }

  .last-store-content {
    flex-grow: 1;
    min-width: 0;
    margin-left: 14rpx;
  }

  .last-store-name {
    margin-bottom: 4rpx;
    font-size: 28rpx;
    color: #242424;
    line-height: 40rpx;
    font-weight: bold;
  }

  .last-store-desc {
    font-size: 28rpx;
    color: #666666;
    line-height: 40rpx;
  }

  .last-store-close {
    position: absolute;
    top: -20rpx;
    right: -2rpx;
    display: flex;
    border-radius: 50%;
    background-color: #ffffff;
  }
}
</style>